import store from "@/store/index.js"
import Vue from "vue";
import utils from "@/ext/utils.js";
import config from "@/config.js"
import Request from "@/ext/http.js";
import User from '../module/user'
import { compatibleStorage } from '@/store/utils'
export default class Init {
	// 获取网络信息
	getNetworkType() {
		uni.getNetworkType({
			success(res) {
				let mNetworkType = '';
				switch (res.networkType) {
					case '2g':
						mNetworkType = '2G';
						break;
					case '3g':
						mNetworkType = '3G';
						break;
					case '4g':
						mNetworkType = '4G';
						break;
					case '5g':
						mNetworkType = '5G';
						break;
					case 'wifi':
						mNetworkType = 'Wifi';
						break;
					default:
						mNetworkType = res.networkType;
						break;
				}
				store.commit('setNetworkType', mNetworkType);
			}
		});

		//监听网络变化
		uni.onNetworkStatusChange(function(res) {
			store.commit('setNetworkType', res.networkType);
			if (res.networkType == 'none') {
				utils.toast('当前网络不可用，请检查你的网络');
			}
		});
	}
	/**
	 * 获取系统信息
	 */
	getSystemInfo() {
		this.systemInfo = uni.getSystemInfoSync();
		store.commit('setSystemInfo', this.systemInfo)
	}
	/**
	 * 设置标题头部高度
	 */
	setHeaderHeight() {
		const screenWidth = this.systemInfo.screenWidth
		const screenHeight = this.systemInfo.screenHeight
		const isIOS = this.systemInfo.system.indexOf('iOS') >= 0
		const isFullScreen = screenHeight >= 812 && screenWidth >= 375;
		let headerHeight = isFullScreen ? 88 : isIOS ? 64 : 70;
		const shareBtn = wx.getMenuButtonBoundingClientRect()
		headerHeight = shareBtn.bottom + uni.upx2px(12)
		store.commit('setMenuButtonPosition', shareBtn)
		store.commit('headerHeight', headerHeight);
	}
	//设置埋点所需参数
	setBuryPoint() {
		if (!compatibleStorage('cookiesId')) {
			store.commit('buryPoint/setCookiesId', store.state.appSystemInfo.cookiesId || utils.generCookieId());
		}
		store.commit('buryPoint/setSessionId', utils.getRandomStr());
	}
	constructor() {
		this.getSystemInfo() // 获取用户系统信息
		this.getNetworkType() // 获取网络状态
		// this.setBuryPoint()
		// #ifdef MP-WEIXIN
		this.setHeaderHeight() // 设置标题栏的高度
		// #endif
	}
}
